package com.onlyxiahui.of.muc;

import java.io.File;

import org.jivesoftware.openfire.IQRouter;
import org.jivesoftware.openfire.XMPPServer;
import org.jivesoftware.openfire.container.Plugin;
import org.jivesoftware.openfire.container.PluginManager;
import org.jivesoftware.openfire.event.SessionEventDispatcher;
import org.jivesoftware.openfire.interceptor.InterceptorManager;
import org.jivesoftware.openfire.muc.MUCEventDispatcher;
import org.jivesoftware.openfire.muc.MUCEventListener;

import com.onlyxiahui.of.muc.iq.IQRoomChatLogHandler;
import com.onlyxiahui.of.muc.iq.IQRoomHandler;
import com.onlyxiahui.of.muc.iq.IQRoomMemberHandler;
import com.onlyxiahui.of.muc.iq.IQRoomOperateHandler;
import com.onlyxiahui.of.muc.iq.IQUserChatLogHandler;
import com.onlyxiahui.of.muc.listener.MUCEventListenerImpl;
import com.onlyxiahui.of.muc.listener.UserSessionEventListener;

public class PerpetualRoomPlugin implements Plugin {
	UserSessionEventListener sessionEventListener = new UserSessionEventListener();
	private MUCEventListener mucEventListener = new MUCEventListenerImpl();
	IQRoomHandler iQRoomHandler = new IQRoomHandler();
	IQRoomMemberHandler iQRoomMemberHandler = new IQRoomMemberHandler();
	IQRoomOperateHandler iQRoomOperateHandler = new IQRoomOperateHandler();

	IQRoomChatLogHandler iQRoomChatLogHandler = new IQRoomChatLogHandler();
	IQUserChatLogHandler iQUserChatLogHandler = new IQUserChatLogHandler();

	MUCPacketInterceptor packetInterceptor = new MUCPacketInterceptor();

	public PerpetualRoomPlugin() {
	}

	@Override
	public void initializePlugin(PluginManager manager, File pluginDirectory) {
		MUCEventDispatcher.addListener(mucEventListener);
		InterceptorManager.getInstance().addInterceptor(packetInterceptor);
		SessionEventDispatcher.addListener(sessionEventListener);
		IQRouter iqRouter = XMPPServer.getInstance().getIQRouter();
		iqRouter.addHandler(iQRoomHandler);
		iqRouter.addHandler(iQRoomMemberHandler);
		iqRouter.addHandler(iQRoomOperateHandler);

		iqRouter.addHandler(iQRoomChatLogHandler);
		iqRouter.addHandler(iQUserChatLogHandler);
	}

	@Override
	public void destroyPlugin() {
		SessionEventDispatcher.removeListener(sessionEventListener);
		InterceptorManager.getInstance().removeInterceptor(packetInterceptor);
		MUCEventDispatcher.removeListener(mucEventListener);
		IQRouter iqRouter = XMPPServer.getInstance().getIQRouter();
		iqRouter.removeHandler(iQRoomHandler);
		iqRouter.removeHandler(iQRoomMemberHandler);
		iqRouter.removeHandler(iQRoomOperateHandler);

		iqRouter.removeHandler(iQRoomChatLogHandler);
		iqRouter.removeHandler(iQUserChatLogHandler);
	}
}
